import java.io.*; 
import java.sql.*; 
import java.text.*; 
import java.util.*; 
 
public class ProcessLifeMap { 
 
    public static final String[] DB_FILE = { "LifeMap_GS1.db", "LifeMap_GS2.db", "LifeMap_GS3.db", "LifeMap_GS4.db", "LifeMap_GS7.db", "LifeMap_GS8.db", "LifeMap_GS9.db", "LifeMap_GS10.db", "LifeMap_GS12.db" }; 
 
    public static final String LOCATION_TABLE = "locationTable"; 
    public static final String LOCATION_KEY_ID = "_node_id"; 
    public static final String LOCATION_KEY_LATITUDE = "_latitude"; 
    public static final String LOCATION_KEY_LONGITUDE = "_longitude"; 
    public static final String LOCATION_KEY_ALTITUDE = "_altitude"; 
    public static final String LOCATION_KEY_ACTIVITY = "_activity"; 
    public static final String LOCATION_KEY_TIME = "_time_location"; 
 
    public static void main ( String args[] ) throws Exception { 
            Class.forName("org.sqlite.JDBC").newInstance(); 
            DecimalFormat df = new DecimalFormat("###.##############"); 
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd aa:KK:mm"); 
            SimpleDateFormat writeFormat = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss");
            SimpleDateFormat sdfTimeStamp = new SimpleDateFormat("yyyyMMddHHmmss"); 
            long previousTime = Long.MIN_VALUE; 
            for ( String file : DB_FILE ) { 
                java.sql.Connection conn = DriverManager.getConnection("jdbc:sqlite:" + file); 
                Statement stm = conn.createStatement(); 
                String query =  "SELECT "  
                        + LOCATION_KEY_ID + ", "  
                        + LOCATION_KEY_LATITUDE + ", " 
                        + LOCATION_KEY_LONGITUDE + ", " 
                        + LOCATION_KEY_ALTITUDE + ", " 
                        + LOCATION_KEY_ACTIVITY + ", " 
                        + LOCATION_KEY_TIME  
                        + " FROM " + LOCATION_TABLE + " "; 
                ResultSet cursor = stm.executeQuery(query); 
                int sequence = 0; 
                while(cursor.next()) { 
                    Double latitude = cursor.getLong(cursor.findColumn(LOCATION_KEY_LATITUDE)) * 0.000001; 
                    Double longitude = cursor.getLong(cursor.findColumn(LOCATION_KEY_LONGITUDE)) * 0.000001; 
                    Double altitude = cursor.getDouble(cursor.findColumn(LOCATION_KEY_ALTITUDE)); 
                    Long activity = cursor.getLong(cursor.findColumn(LOCATION_KEY_ACTIVITY)); 
                    java.util.Date time = sdfTimeStamp.parse(cursor.getString(cursor.findColumn(LOCATION_KEY_TIME))); 
                    if ( ( time.getTime() - previousTime ) >= 5 * 60 * 60 * 1000 ) sequence++; // More than five hours since the last register in the log
                    previousTime = time.getTime(); 
                    if ( latitude == 0 && longitude == 0) continue; 
                    System.out.println('"' + file + '.' + sequence + '"' + ',' + df.format(latitude) + ',' + df.format(longitude) + ',' + 0 + ',' + altitude + ',' + '"' + writeFormat.format(time) + '"' + ',' + time.getTime()); 
                } 
                cursor.close(); 
            } 
    } 
     
}